<?php
class Merchant_PlanController extends Zend_Controller_Action
{

	public function preDispatch(){
		$this->tplDirectory = "merchant/plan/";		 
		$customerService = new Merchant_Service_Customer();
		$this->customer = $customerService->getLoginInfo()->customer;
	}
	
	public function editAction(){
		$planService = new Merchant_Service_Plan();
		
		//查找当前 计划使用套餐
		$plan = $planService->getCurrentPlan($this->customer['customer_id']);
		if(substr($plan['pallet_qty'],-3) == ".00"){
			$plan['pallet_qty'] = substr($plan['pallet_qty'],0,-3);
		}
		$this->view->plan = $plan;
		$this->view->dateMonth = date("M Y",strtotime("+1 month"));
		
		//查找当前 使用套餐
		$planUsage = $planService->getCurrentPlanUsage($this->customer['customer_id']);
		if(substr($planUsage['pallet_qty'],-3) == ".00"){
			$planUsage['pallet_qty'] = substr($planUsage['pallet_qty'],0,-3);
		}
		$this->view->planUsage = $planUsage;
		if(!$this->_request->isPost()){
			
			$items = $planService->getRateByTypeAndQty("item",999998);
			$pallets = $planService->getRateByTypeAndQty("pallet",999998);
			$skus = $planService->getRateByTypeAndQty("sku",999998);
			foreach($items as $key => $value){
				if(substr($value['range_end'],-3) == ".00"){
					$items[$key]["range_end"] = substr($value['range_end'],0,-3);
				}
			}
			foreach($pallets as $key => $value){
				if(substr($value['range_end'],-3) == ".00"){
					$pallets[$key]["range_end"] = substr($value['range_end'],0,-3);
				}
			}
			foreach($skus as $key => $value){
				if(substr($value['range_end'],-3) == ".00"){
					$skus[$key]["range_end"] = substr($value['range_end'],0,-3);
				}
			}
			array_pop($items);
			array_pop($pallets);
			array_pop($skus);
			$this->view->items = $items;
			$this->view->pallets = $pallets;
			$this->view->skus = $skus;
			
			echo Cff::renderTpl($this->tplDirectory.'plan-edit.tpl','user-layout');
			exit;
		}
		$skus = $this->_request->sku;
		$pallets = $this->_request->pallet;
		$items = $this->_request->item;
		$result = $planService->changePlan($this->customer['customer_id'],$skus,$pallets,$items);
		if($result == 0){
			die(json_encode(array(
				"ask" => true,
				"result" => "Update successful",
			)));
		}else if($result == 2){
			die(json_encode(array(
				"ask" => false,
				"result" => "已超出当前使用量,更新失败",
			)));
		}else{
			die(json_encode(array(
				"ask" => false,
				"result" => "未知错误,更新失败",
			)));
		}
	}
	
	public function listAction(){
		$planService = new Merchant_Service_Plan();		
		
		$plan = $planService->getCurrentPlan($this->customer['customer_id']);				//查找当前 计划使用套餐
				
		$planUsage = $planService->getCurrentPlanUsage($this->customer['customer_id']);		//查找当前 使用套餐
		
		$this->view->dateMonth = date("M Y",strtotime("+1 month"));
		$this->view->startDate = date('Y-m-d',strtotime("-1 month")); 
		$this->view->endDate = date('Y-m-d');
		$this->view->plan = $plan;
		$this->view->planUsage = $planUsage;
		echo Cff::renderTpl('merchant/plan/plan-list.tpl','user-layout');
	}
	
	public function planUsageListAction(){
	 
		$planService = new Merchant_Service_Plan();	
		
		$startTime = $this->_request->getParam('startTime',date('Y-m-d',strtotime("-1 month"))); 
		$endTime =  $this->_request->getParam('endTime',date('Y-m-d'));
		$page = $this->_request->getParam("page",1);
		$pageSize = $this->_request->getParam("pageSize",20);
		$type = $this->_request->getParam('type','');
		$sortColumn = $this->_request->getParam('sortColumn','');
		$sortMethod = $this->_request->getParam('sortMethod','');
		$orderby='';
		if($sortColumn){
			$orderby = $sortColumn.' '.$sortMethod;
		}
		
		$option["customer_id"] = $this->customer['customer_id'];
		$option['expired-start'] = $startTime;
		$option['expired-end'] = $endTime;
		$option['active'] = '0';
		 
		$count = $planService->getPlanUsageList($option,'count(*)');	
 
		$planDetail = $planService->getPlanUsageList($option,'*',$orderby,$page,$pageSize);	
		foreach($planDetail as $key=>$val){
			$plan = $planService->getPlanById($val['plan_id']);
			$planDetail[$key]['total']  = $plan->total;
			$planDetail[$key]['currency_code']  = $plan->currency_code;
			$planDetail[$key]['expired'] =  date('Y-m-d',strtotime($val['expired']));
		
		}

	    if(is_array($planDetail)&$planDetail){
			$result['ask'] = "Success"; 
		}
		 
		$result['count'] = $count;
		$result['data']  = $planDetail;
		echo Zend_Json::encode($result);
		 
	}
	
	public function planUsageDetailAction(){
		$typeArr = Merchant_Service_PlanUsageDetail::$typeArr;
		$this->view->typeArr = $typeArr;
		$id = $this->_request->getParam('id','');	//指定的权限名ID
		$this->view->pu_id = $id;
		
		if($this->_request->isPost()){
			$customerService = new Merchant_Service_Customer();
			$customerInfo = $customerService->getLoginInfo()->customer;
			$customer_id = $customerInfo['customer_id'];
			if(!$customer_id)return false;
						
			$cond =array();
			$pu_id = $this->_request->getParam('pu_id','');
			$type = $this->_request->getParam('type','');
			$ref_id = $this->_request->getParam('ref_id','');
			$pu_id && $cond['pu_id'] = $pu_id;
			$type && $cond['type'] = $type;
			$ref_id && $cond['ref_id'] = $ref_id;
			$cond['customer_id'] = $customer_id; 
			
			$page = $this->_request->getParam('page',1);
			$pageSize = $this->_request->getParam('pageSize',20);
			$page = $page?$page:1;
			$pageSize = $pageSize?$pageSize:20;
			$data =array('ask'=>0,'total'=>0);	
			
			$total = Merchant_Service_PlanUsageDetail::getByCond($cond,'count(*)','pud_id',0,0);
			$rows = Merchant_Service_PlanUsageDetail::getByCond($cond,'*','pud_id',$pageSize,($page-1)*$pageSize);
			
			if(!$total){ die(json_encode($data)); }
			
			$data['ask'] = 1;
			$data['total'] = $total;
			$data['result'] = $rows;
			
			die(json_encode($data));
			
		}
		echo Cff::renderTpl('merchant/plan/plan-usage-detail.tpl','layout-blank');
		
	}
	
	
}